/**
 * 
 */
package com.alkandros.linesmash.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.alkandros.linesmash.R;
import com.alkandros.linesmash.models.Location;
import com.alkandros.linesmash.models.Venues;

/**
 * @author Jiju Induchoodan
 * 
 */
public class VenuesAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<Venues> venuesArray;
	private ArrayList<Location> locationArray;
	private LayoutInflater inflater;
	private int type = 0;
	private Venues venueModel;
	private Location locationModel;

	public VenuesAdapter(Context ctx, int dataType) {
		type = dataType;
		context = ctx;
		inflater = LayoutInflater.from(context);
	}

	public void setLocationData(ArrayList<Location> data) {
		locationArray = data;
	}

	public void setVenuesData(ArrayList<Venues> data) {
		venuesArray = data;
	}

	static class ViewHolder {
		TextView txtName;
		TextView txtDistance;
	}

	@Override
	public int getCount() {
		return type == 0 ? venuesArray.size() : locationArray.size();
	}

	@Override
	public Object getItem(int position) {
		return type == 0 ? venuesArray.get(position) : locationArray
				.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		View rowView = convertView;
		int distance=0;
		if (type == 0) {
			venueModel = (Venues) this.getItem(position);
		} else {
			locationModel = (Location) this.getItem(position);
		}
		if (rowView == null) {
			ViewHolder viewHolder = new ViewHolder();
			rowView = inflater.inflate(R.layout.location_list_item, null);

			viewHolder.txtName = (TextView) rowView.findViewById(R.id.txt_name);
			viewHolder.txtDistance = (TextView) rowView
					.findViewById(R.id.txt_distance);
			rowView.setTag(viewHolder);
		}
		ViewHolder holder = (ViewHolder) rowView.getTag();
	//	Log.e("", "Title"+ venueModel.getTitle());
		holder.txtName.setText(type == 0 ? venueModel.getTitle()
				: locationModel.getTitle());
		
		if(type==0 && venueModel.getDistance()!=null && !venueModel.getDistance().equals(""))
		{
			distance = Integer.parseInt(venueModel.getDistance())/1000;
			
			holder.txtDistance
			.setText(String.valueOf(distance)+"Kms");
		}
		/*else if(type==1 && locationModel.getDistance()!=null && !locationModel.getDistance().equals(""))
		{
			distance = Integer.parseInt(locationModel.getDistance())/1000;
		}*/
		
		return rowView;
	}

}
